﻿using System.Collections.Generic;
using System.Data;
using KMF.Common;

namespace KMF.DAL
{
    public partial  class ProductTagDb
    {
        /// <summary>
        /// Thêm các Tag vào bảng ProductTag
        /// </summary>
        /// <param name="nameList"></param>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static int AddProductTag(string productId, string nameList)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_ProductTag_AddProductTag");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ProductId", productId));
                command.Parameters.Add(dao.CreateParameter("@NameList", nameList));
                var result = command.ExecuteNonQuery();
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Cập nhật và Insert lại các Tags sau đó Insert dữ liệu vào bảng ProductTagMapping theo ProductId
        /// </summary>
        /// <param name="nameList"></param>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static int UpdateProductTag(string productId, string nameList)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_ProductTag_UpdateProductTag");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ProductId", productId));
                command.Parameters.Add(dao.CreateParameter("@NameList", nameList));
                var result = command.ExecuteNonQuery();
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Lấy về danh sách các Tags của Product
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static List<ProductTagDto> GetProductTagByProductId(string productId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_ProductTagMapping_GetByProductId");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ProductId", productId));
                return dao.GetDTOList<ProductTagDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Xoá các bản ghi trong bảng ProductTagMapping theo productId
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static int DeleteProductTagMappingByProductId(string productId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_ProductTagMapping_DeleteByProductId");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ProductId", productId));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Lấy về danh sách Tag Name từ bàng ProductTag và phân trang
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="totalPage"></param>
        /// <param name="totalRecord"></param>
        /// <param name="itemsPerPage"></param>
        /// <returns></returns>
        public static List<ViewProductTagListDto> GetProductTagListPaging(int currentPage, out int totalPage, out int totalRecord, int itemsPerPage)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_ProductTag_GetAllPaging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@CurrentPage", currentPage));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));

                var total_page = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                total_page.Direction = ParameterDirection.Output;

                var total_record = command.Parameters.Add("@TotalRecord", SqlDbType.Int);
                total_record.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<ViewProductTagListDto>(ref command);

                totalPage = int.Parse(total_page.Value.ToString() == "" ? "0" : total_page.Value.ToString());
                totalRecord = int.Parse(total_record.Value.ToString() == "" ? "0" : total_record.Value.ToString());

                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Lấy về bản ghi ProductTag theo Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static ProductTagDto GetById(int id)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_ProductTag_GetById");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Id", id));
                return dao.GetSingleDTO<ProductTagDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Cập nhật bản ghi trong bảng ProductTag
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int UpdateProductTag(ProductTagDto model)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_ProductTag_Update");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Id", model.Id));
                command.Parameters.Add(dao.CreateParameter("@Name", model.Name));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}
